/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrl.mml;

/**
 *
 * @author Mr.Sam
 */
import java.io.*;
import java.util.*;

import fr.ove.openmath.jome.ctrl.linear.events.*;
import fr.ove.openmath.jome.ctrl.mml.*;

/**
* A parser for MathML objects.
*
* @author © 2000 DIRAT Laurent
* @version 1.0 25/05/2000
*/
public class MathMLParser extends BasicXMLParser implements Serializable, LinearParserEventSource {
    /**
    * The handler for the parser.
    */
    private MathMLParserHandler handler = new MathMLParserHandler();
    
    /**
    * The list of listeners of the instance
    */
    private Vector listeners = new Vector();
    
    /**
    * The constructor.
    */
    public MathMLParser() {
        handler.setEventSource(this);
    }
    
        
    /**
    * Registers another listener of the instance.
    * @param linearParserListener the listener to add.
    */
    public synchronized void addLinearParserListener(LinearParserListener linearParserListener) {
        listeners.addElement(linearParserListener);
    }

    /**
    * Removes a listener.
    * @param linearParserListener the listener to remove.
    */
    public synchronized void removeLinearParserListener(LinearParserListener linearParserListener) {
        listeners.removeElement(linearParserListener);
    }

    public void parseMathMLObject(String mmlObject) {
        ByteArrayInputStream in = new ByteArrayInputStream(mmlObject.getBytes());
        try {
            parse(in);
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
    
    /**
    * Fires a LinearParserEvent event to registered listeners.
    * @param linearParserEvent event encapsulating relevant information.
    */
    public void fireLinearParserEvent(LinearParserEvent linearParserEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((LinearParserListener) listeners.elementAt(i)).consumeLinearParserEvent(linearParserEvent);
    }
    
    /**
    * Called by the parser when an tag has been parsed.
    * @param tag the parsed tag.
    */
    public void readTag(Tag tag) {
        String name = tag.getName();
        
        if (tag.isStart()) {
            if (name.equals("mathml"))
                handler.startObject();
            else if (name.equals("apply"))
                handler.startApplication();
            else if (name.equals("lambda"))
                handler.startLambda();
            else if (name.equals("bvar"))
                handler.startBVars();
            else
                handler.startElement(tag);
        }
        else {
            if (name.equals("mathml"))
                handler.endObject();
            else if (name.equals("apply"))
                handler.endApplication();
            else if (name.equals("lambda"))
                handler.startLambda();
            else if (name.equals("bvar"))
                handler.startBVars();
            else
                handler.endElement(tag);
        }
    }
    
    /**
    * Called by the parser when the value of an element has been parsed.
    * @param vale the parsed value.
    */
    public void readValue(String value) {
        handler.readValue(value);
    }
}
